<template>
	<view>
		<u-navbar :border-bottom="false" back-icon-name="home" title-bold title="抢单中心" :custom-back="customBack"></u-navbar>
		<u-sticky>
			<!-- 只能有一个根元素 -->
			<u-tabs :list="list" :is-scroll="false" :current="current" @change="change"></u-tabs>
		</u-sticky>
		<riderList :list="orderList" :isScan="isScan" :userInfo="userInfo" :current="current"
			@getOrderTakeawayData='getOrderTakeawayData'></riderList>
	</view>
</template>

<script>
	import orderList from './components/order-list.vue';
	import riderList from './components/rider-list.vue';
	import WXJsSDK from '@/utils/wxSdk.js';
	import {
		mapState,
		mapMutations
	} from 'vuex';
	export default {
		props: {
			current: {
				type: [Number, String],
				default: 0
			}
		},
		data() {
			return {
				page: 1, //页数
				isScan: undefined, // 扫码按钮应该出现的位置
				list: [{
					name: '抢单大厅'
				}, {
					name: '取餐中',
				}, {
					name: '配送中',
				}, {
					name: '已完成'
				}],
				orderList: [],
				userInfo: {}
			}
		},
		// computed: {
		// 	...mapState(['hasLogin', 'userInfo']),
		// },
		components: {
			orderList,
			riderList
		},
		onLoad() {
			try {
				if (process.env.NODE_ENV === 'production') {
					WXJsSDK.init({
						APIs: ['scanQRCode']
					});
				}
			} catch (e) {
				//TODO handle the exception
			}
		},
		async onShow() {
			this.userInfo = await uni.getStorageSync('riderUserInfo');
			if (!uni.getStorageSync('riderToken')) {
				this.$ut.route('pages/login/login', {}, 'reLaunch');
				return false;
			}
			if (this.userInfo.workType == null) {
				this.$ut.route('pages/rider/index', {}, 'tab');
				return false;
			}
			if (this.userInfo.workType == 0) {
				this.isScan = 0;
				this.list = [{
					name: '配送中',
				}, {
					name: '已完成'
				}];
			} else {
				this.isScan = 2;
				this.list = [{
					name: '抢单大厅'
				}, {
					name: '取餐中',
				}, {
					name: '配送中',
				}, {
					name: '已完成'
				}];
			}
			this.getOrderTakeawayData();
			//console.log(e.current);
			// if(e.current) {
			// 	// 组件中用了全等，此处要转number
			// 	this.current = Number(e.current);
			// 	this.initData();
			// }
		},
		// 下拉刷新
		onPullDownRefresh() {
			this.page = this.page + 1;
			this.getOrderTakeawayData();
		},
		methods: {
			...mapMutations(['persistenceVuex']),
			// 获取订单
			getOrderTakeawayData() {
				this.orderList = [];
				if (this.current == 0) {
					if (this.userInfo.workType == 0) {
						this.$u.api.getOrderTakeawayToDelivery({
							deliveryState: 2,
							deliveryId: this.userInfo.id,
							page: this.page
						}).then(res => {
							this.orderList = res.dataList;
						})
					} else {
						const deliveryId = this.userInfo.id;
						try{
							this.$u.api.getOrderTakeawayHallToDelivery({
								deliveryState: this.current,
								deliveryId,
								page: this.page
							}).then(res => {
								this.orderList = res.dataList;
							})
						}catch(err){
							//TODO handle the exception
							console.log(err);
						}
					}

				}
				if (this.current == 1) {
					this.$u.api.getOrderTakeawayToDelivery({
						deliveryState: this.userInfo.workType == 0 ? 3 : 1,
						deliveryId: this.userInfo.id,
						page: this.page
					}).then(res => {
						this.orderList = res.dataList;
					})
				}
				if (this.current == 2) {
					this.$u.api.getOrderTakeawayToDelivery({
						deliveryState: 2,
						deliveryId: this.userInfo.id,
						page: this.page
					}).then(res => {
						this.orderList = res.dataList;
					})
				}
				if (this.current == 3) {
					this.$u.api.getOrderTakeawayToDelivery({
						deliveryState: 3,
						deliveryId: this.userInfo.id,
						page: this.page
					}).then(res => {
						this.orderList = res.dataList;
					})
				}
				uni.stopPullDownRefresh();
			},
			initData() {
				this.getOrderTakeawayData();
			},
			change(index) {
				this.current = index;
				this.page = 1;
				this.getOrderTakeawayData();
			},
			// 跳转个人中心
			customBack() {
				this.$ut.route('pages/rider/index');
			}
		}
	}
</script>

<style lang="scss" scoped>

</style>